package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.ZyCommunity;
import com.blb.community_admin.service.IZyCommunityService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Api(tags = "小区模块")
@RestController
public class CommunityController {
    @Autowired
    private IZyCommunityService iZyCommunityService;

    /**
     * 列表显示
     * @return
     */
    @ApiOperation("小区列表显示")
    @GetMapping("/community")
    public ResponseResult<List<ZyCommunity>> selectAll(){
        return ResponseResult.ok(iZyCommunityService.list(null));
    }

    @ApiOperation("单表模糊查询")
    @PostMapping("/communitying/pages")
    public ResponseResult<IPage<ZyCommunity>> selectCommunitys(Long current,Long size,@RequestBody ZyCommunity ZyCommunity ) {
        IPage<ZyCommunity> zyCommunityIPage = iZyCommunityService.selectCommunitys(current,size,ZyCommunity);
        return ResponseResult.ok(zyCommunityIPage);
    }
    /**
     * 添加小区
     *
     * @param zyCommunity
     * @return
     */
    @Log(module = "小区", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加小区")
    @PostMapping("/community")
    public ResponseResult<String> addCommunity(@RequestBody ZyCommunity zyCommunity) {
        zyCommunity.setDeptId(103L);
        zyCommunity.setCreateTime(LocalDateTime.now());
        boolean save = iZyCommunityService.save(zyCommunity);
        System.out.println(save);
        return ResponseResult.ok("添加成功");
    }

    /**
     * 修改
     *
     * @param zyCommunity
     * @return
     */
    @Log(module = "小区", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改")
    @PutMapping("/community")
    public ResponseResult<String> updateCommunity(@RequestBody(required = false) ZyCommunity zyCommunity) {
        zyCommunity.setUpdateTime(LocalDateTime.now());
        iZyCommunityService.updateById(zyCommunity);
        return ResponseResult.ok("修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Log(module = "小区", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除")
    @DeleteMapping("/community/{id}")
    public ResponseResult<String> deleteCommunity(@PathVariable Long id) {
        iZyCommunityService.removeById(id);
        return ResponseResult.ok("删除成功");
    }

    /**
     * 多删
     * @return
     */
    @Log(module = "小区", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("多删")
    @PostMapping("/deleteMany")
    public ResponseResult<String> deleteMany(@RequestBody Long[] community){
        List<Long> list = Arrays.asList(community);
        iZyCommunityService.removeByIds(list);
        return ResponseResult.ok("删除成功");
    }

    /**
     * 更换物业
     * @param zyCommunity
     */
    @Log(module = "小区", operationType = 2, requestMethod = "PUT")
    @ApiOperation("更换物业")
    @PutMapping("/community/dept")
    public ResponseResult<String> updateDept(@RequestBody ZyCommunity zyCommunity){
        iZyCommunityService.updateDept(zyCommunity);
        return ResponseResult.ok("修改成功");
    }

    @Log(module = "小区", operationType = 5, requestMethod = "POST")
    @ApiOperation("导出")
    @PostMapping("/community/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<ZyCommunity> zyCommunities) throws IOException {
        String path = EasyExcelUtil.write("小区数据", ZyCommunity.class, zyCommunities);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }
}
